/**
 * Majiang is a library that implements Mahjong game rules.
 *
 * Copyright 2009 Prudent Jérome
 *
 *     This file is part of Majiang.
 *
 *     Majiang is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Majiang is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * You can contact me at jprudent@gmail.com
 */
package org.liprudent.majiang.engine.tile.impl;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.liprudent.majiang.engine.tile.ITile;
import org.liprudent.majiang.engine.tile.TileFamily;

abstract public class AbstractTile implements ITile {
	private final TileFamily family;

	/**
	 * id can be 0 to 3.
	 */
	private final int id;

	public AbstractTile(final TileFamily family, final int id) {
		if (id < 0 || id > 3) {
			throw new IllegalArgumentException("(family="+family+",id="+id+")Id must be between 0 and 3");
		}
		this.family = family;
		this.id = id;
	}

	public TileFamily getFamily() {
		return family;
	}

	public int getId() {
		return id;
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public boolean equals(final Object obj) {
		return EqualsBuilder.reflectionEquals(obj, this);
	}

	@Override
	public String toString() {
		return getUniqueId();
	}

	@Override
	public String getUniqueId() {
		return "family=" + family + "|id=" + id;
	}
	
	@Override
	public int compareTo(ITile o) {
		//consistent with equals
		if(this.equals(o)) return 0;
		
		else return getCloneValue() + getId() - o.getCloneValue() - o.getId();
	}
	
	@Override
	public boolean compare(final ITile obj) {
		if(obj == null) return false;
		return getCloneValue() - obj.getCloneValue() == 0;
	}

}
